package com.springcloud.requestcache;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.springcloud.common.Friend;
import com.springcloud.service.MyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: 顾志杰
 */
@Slf4j
@Service
public class RequestCacheService {

    @Autowired
    private MyService iService;

    /**
     *  HystrixCommand注解中的fallbackMethod参数在同级类里写的降级方法参数要一致
     *  CacheResult 将方法返回值存入到上下文中
     *  commandKey 指定这个返回值的key
     * @param name
     * @return
     */
    @CacheResult
    @HystrixCommand(commandKey = "cacheKey")
    public Friend RequestCache(@CacheKey String name){
        log.info("request cache " + name);
        Friend friend = Friend.builder().name(name).build();
        friend = iService.sayHiPost(friend);
        log.info("after request cache " + name);
        return friend;
    }

}
